package com.mybatisflex.example.controller;

import com.mybatisflex.example.controller.response.DistrictCityRsp;
import com.mybatisflex.example.controller.response.DistrictRsp;
import com.mybatisflex.example.pojo.entity.BizResult;
import com.mybatisflex.example.service.DistrictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "行政区API")
@Slf4j
@RestController
public class DistrictController {

    @Autowired
    private DistrictService districtService;

    @Operation(summary = "行政区域查询")
    @Parameters({
            @Parameter(name = "keywords", description = "规则：只支持单个关键词语搜索关键词支持：行政区名称、adcode"),
            @Parameter(name = "subdistrict", description = "设置显示下级行政区级数（行政区级别包括：0国家、1省/直辖市、2市、3区/县、4镇/乡/街道）",
                       examples = {@ExampleObject(name = "不返回下级行政区", value = "0"),
                               @ExampleObject(name = "返回下一级行政区", value = "1"),
                               @ExampleObject(name = "返回下两级行政区", value = "2"),
                               @ExampleObject(name = "返回下三级行政区", value = "3")})
    })
    @GetMapping(value = "/district")
    public BizResult<List<DistrictRsp>> district(@RequestParam(required = false) String keywords,
                                                 @RequestParam(required = false, defaultValue = "0") Integer subdistrict) {
        List<DistrictRsp> list = districtService.district(keywords, subdistrict);
        return BizResult.create(list);
    }

    @Operation(summary = "行政区域查询-首字母")
    @Parameters({
            @Parameter(name = "subdistrict", description = "设置显示行政区级数",
                       examples = {
                               @ExampleObject(name = "返回市级", value = "2"),
                               @ExampleObject(name = "返回市级及县级", value = "3")})
    })
    @GetMapping(value = "/district/initial")
    public BizResult<List<DistrictCityRsp>> initial(@RequestParam(required = false, defaultValue = "2") Integer subdistrict) {
        List<DistrictCityRsp> list = districtService.initial(subdistrict);
        return BizResult.create(list);
    }

    @Operation(summary = "行政区域查询-行政区级别")
    @GetMapping(value = "/district/level")
    public BizResult<List<DistrictRsp>> getDistrictByLevel(@RequestParam(required = false, defaultValue = "2") Integer level) {
        List<DistrictRsp> list = districtService.getDistrictByLevel(level);
        return BizResult.create(list);
    }
}
    